package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.SaveProductTemplateBean;
import com.ymatou.iapi.productmanager.service.SaveProductTemplateCall;
import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.sqlwapper.YmtProductDescExtraWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.*;

public class Ts_SaveProductTemplate {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
	private static YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
	private static SaveProductTemplateBean saveProductTemplateBean = new SaveProductTemplateBean();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建/编辑商品模版");
		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveProductTemplateCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_001() {
		Logger.start(true, "创建商品模版,保存模板");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setProductTemplateId(null);
			saveProductTemplateBean.setDescription("测试模板");
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(2);
			
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Thread.sleep(5000);
			Logger.verifyEquals("200", saveProductTemplateCall.getString("Code"), "验证Code");
			int tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_002() {
		Logger.start(true, "修改商品模版");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setDescription("测试模板");
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(2);
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			int tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
			
			saveProductTemplateBean.setProductTemplateId(tempId);
			saveProductTemplateBean.setDescription("测试修改模板");
			saveProductTemplateCall.callService();
			Logger.verifyEquals("200", saveProductTemplateCall.getString("Code"), "验证Code");
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	/**
	 * 已无上线
	 */
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_SaveProductTemplate_003() {
		Logger.start(true, "一个类型模板上限不超过50");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setDescription("测试模板");
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(2);
			saveProductTemplateBean.setProductTemplateId(null);
			
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Thread.sleep(5000);
			int tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
			for (int i=0;i<22;i++){
				saveProductTemplateBean.setDescription("测试模板"+TestDataManager.getRandomIntNum(10));
				saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
				saveProductTemplateBean.setProductTemplateId(null);
				saveProductTemplateBean.setSellerId(sellerId);
				saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
				saveProductTemplateBean.setTemplateType(2);
				
				saveProductTemplateCall.setData(saveProductTemplateBean);
				saveProductTemplateCall.callService();
				tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
				checkSaveProductTemplate(tempId,saveProductTemplateBean);
			}
			saveProductTemplateBean.setDescription("测试模板"+TestDataManager.getRandomIntNum(10));
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setProductTemplateId(null);
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(2);
			
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Logger.verifyEquals("200", saveProductTemplateCall.getString("Code"), "返回报文code");
			tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
		}catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_004() {
		Logger.start(true, "修改商品模版,敏感词校验");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setDescription("测试模板，毛泽东");
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(2);
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Logger.verifyEquals("201", saveProductTemplateCall.getString("Code"), "敏感词校验");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_005() {
		Logger.start(true, "模板关联多个商品，修改模板。");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			
			SellerProductCallService.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = SellerProductCallService.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = SellerProductCallService.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = SellerProductCallService.saveTemplate(sellerId, 3);
			List<ProdTplBean> ProdTplBeanList = Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate);
			
			//发布商品
			List<String> productIdList = new ArrayList<>();
			for(int i=0;i<3;i++){
				JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc12.json");	
				Map<String, Object> updateMap = MapUtil.hashMap();
				updateMap.put("UserID", sellerId);
				updateMap.put("LoginID", sellerName);
				updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
				updateMap.put("Product->BuyerNoticeId", buyerNoticeTemplate.getTemplateId());
				updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());
				updateMap.put("Product->iThirdCategoryId", 1123);
				JsonBeanHelper.updateJsonBean(addProductBean, updateMap);
				//call&获取返回报文
				JSONObject result = SellerProductCallService.addNewProduct(addProductBean);
				//productIdList.add(result.getJSONObject("Data").getString("ProductId"));
			}
			
			
			//check product template before update template
			productIdList.forEach(productId->{
				try {
					Logger.debug("=========ProductId:[ "+ productId +" ]================");
					SellerProductCallService.checkTemplateWithProd(productId, ProdTplBeanList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			
			
			//modify
			ProdTplBeanList.forEach(prodTplBean->{
				
				String p1 = "http://pic1.ymatou.com/G01/shangou/M00/31/92/rBB"+Calendar.getInstance().getTimeInMillis()+".jpg";
				String p2 = "http://pic1.ymatou.com/G01/shangou/M00/22/92/rBB"+Calendar.getInstance().getTimeInMillis()+".jpg";
				List<String> picList =Arrays.asList(p1,p2);
				Map<String, Object> updateMap = MapUtil.hashMap();
				updateMap.put("SellerId", sellerId);
				updateMap.put("ProductTemplateId", prodTplBean.getTemplateId());
				updateMap.put("TemplateName", "updateName");
				updateMap.put("Description", "updateDesc");
				updateMap.put("PicUrls", new JSONArray(picList));
				saveProductTemplateCall.callAndGetReturnData(updateMap);
				
				prodTplBean.setTemplateName("updateName");
				prodTplBean.setTemplateDesc("updateDesc");
				prodTplBean.setPicUrls(picList);
		
				
			});
			
			//check product template after update template
			productIdList.forEach(productId->{
				try {
					Logger.debug("=========ProductId:[ "+ productId +" ]================");
					SellerProductCallService.checkTemplateWithProd(productId, ProdTplBeanList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_006() {
		Logger.start(true, "创建商品模版 desc有敏感词");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId,
									"Description","毛主席"));
			
			Logger.verifyEquals(201, ret.getInt("Code"), "返回报文code");
			
			Logger.verifyEquals(true,ret.getString("Msg").contains("毛主席"),"创建商品模版 desc有敏感词");
			

		}catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_007() {
		Logger.start(true, "创建商品模版,保存模板templateType4,编辑模板");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setProductTemplateId(null);
			saveProductTemplateBean.setDescription("测试模板");
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(4);

			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Thread.sleep(5000);
			Logger.verifyEquals("200", saveProductTemplateCall.getString("Code"), "验证Code");
			int tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
			//编辑
			saveProductTemplateBean.setProductTemplateId(tempId);
			saveProductTemplateBean.setDescription("编辑");
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Logger.verifyEquals("200", saveProductTemplateCall.getString("Code"), "验证Code");

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_008() {
		Logger.start(true, "一个类型模板上限超过50-5选购指南图文商品-图文不限制");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setDescription("测试模板");
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(5);
			saveProductTemplateBean.setProductTemplateId(null);
			
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Thread.sleep(5000);
			int tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
			for (int i=0;i<50;i++){
				saveProductTemplateBean.setDescription("测试模板"+TestDataManager.getRandomIntNum(10));
				saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
				saveProductTemplateBean.setProductTemplateId(null);
				saveProductTemplateBean.setSellerId(sellerId);
				saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
				saveProductTemplateBean.setTemplateType(5);
				
				saveProductTemplateCall.setData(saveProductTemplateBean);
				saveProductTemplateCall.callService();
				tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
				checkSaveProductTemplate(tempId,saveProductTemplateBean);
			}
			saveProductTemplateBean.setDescription("测试模板"+TestDataManager.getRandomIntNum(10));
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setProductTemplateId(null);
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(5);
			
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Logger.verifyEquals("200", saveProductTemplateCall.getString("Code"), "返回报文code");
			tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
		}catch (Exception e) {
			Logger.fail(e);
		}
	} 
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_009() {
		Logger.start(true, "5选购指南图文商品-Description可以不传");
		try{
			SellerProductCallService.deleteTemplate(sellerId);
			saveProductTemplateBean.setDescription(null);
			saveProductTemplateBean.setPicUrls(Arrays.asList("http://pic1.ymatou.com/G01/shangou/M00/3E/27/rBBlD1tpVJCAHMkLAAH4S4MXnxE909_375_667_n_w_o.jpg"));
			saveProductTemplateBean.setSellerId(sellerId);
			saveProductTemplateBean.setTemplateName("Test_00" + TestDataManager.getRandomIntNum(5));
			saveProductTemplateBean.setTemplateType(5);
			saveProductTemplateBean.setProductTemplateId(null);
			
			saveProductTemplateCall.setData(saveProductTemplateBean);
			saveProductTemplateCall.callService();
			Thread.sleep(5000);
			int tempId = saveProductTemplateCall.getJsonObject().get("Data").getAsJsonObject().get("ProductTemplateId").getAsInt();
			checkSaveProductTemplate(tempId,saveProductTemplateBean);
		}catch (Exception e) {
			Logger.fail(e);
		}
	} 
	public static void checkSaveProductTemplate(Integer tempId,SaveProductTemplateBean saveProductTemplateBean) throws Exception{
		YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
		List<Map> productTemplate = ymtProductsIWapper.getProductTemplate(tempId);
		Logger.verifyEquals(saveProductTemplateBean.getSellerId(), (Integer)productTemplate.get(0).get("sellerId"), "验证sellerId");
		if(saveProductTemplateBean.getDescription()!=null) {
			Logger.verifyEquals(saveProductTemplateBean.getDescription(), productTemplate.get(0).get("description").toString(), "验证description");
		}
		Logger.verifyEquals(saveProductTemplateBean.getPicUrls().get(0), productTemplate.get(0).get("pic").toString(), "验证pic");
		Logger.verifyEquals(saveProductTemplateBean.getTemplateName(), productTemplate.get(0).get("tempName").toString(), "验证tempName");
		Logger.verifyEquals(saveProductTemplateBean.getTemplateType(),(Integer)productTemplate.get(0).get("type"), "验证type");
	}
	
}
